package com.ruoyi.kenuo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.kenuo.domain.Equ;
import com.ruoyi.kenuo.domain.EquVo;
import com.ruoyi.kenuo.domain.LunBo;
import com.ruoyi.kenuo.mapper.EquMapper;
import com.ruoyi.kenuo.mapper.KernelMeasureRecordMapper;
import com.ruoyi.kenuo.mapper.KernelPendiFkMapper;
import com.ruoyi.kenuo.mapper.KernelUserEquMapper;
import com.ruoyi.kenuo.service.IEquService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备管理Service业务层处理
 *
 * @author polo
 * @date 2022-08-23
 */
@Service
public class EquServiceImpl extends ServiceImpl<EquMapper, Equ> implements IEquService
{
    @Autowired
    private EquMapper equMapper;
    @Autowired
    private KernelMeasureRecordMapper recordMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private KernelPendiFkMapper fkMapper;

    @Autowired
    private KernelUserEquMapper kernelUserEquMapper;


    @Autowired
    private KernelPendiFkMapper kernelPendiFkMapper;



    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public Equ selectEquById(Long id)
    {
        return equMapper.selectEquById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param equ 设备管理
     * @return 设备管理
     */
    @Override
    public List<Equ> selectEquList(Equ equ)
    {
        return equMapper.selectEquList(equ);
    }

    /**
     * 新增设备管理
     *
     * @param equ 设备管理
     * @return 结果
     */
    @Override
    public int insertEqu(Equ equ)
    {
        //绑定状态 1:未绑定  2：已绑定
        equ.setBoundStatus(1);
        equ.setCreateTime(DateUtils.getNowDate());
        //底座盒上报提醒开关
        equ.setDiRemindSwitch("2");
        equ.setDiDiwenRemind("30");
        equ.setDiGaoRemind("38.5");
        equ.setTemDiMode("0");
        equ.setTemBu("0");
        //底座时间显示模式 0:24小时制  1:12小时制
        equ.setShowModeTime("0");
        //体温测量时温度显示模式 0：仅显示体温  1：体温和室温  2023.10.9 确定的在6楼
        equ.setTemShowMode("1");
        //底座显示的温度单位 0：代表C  1：华氏度
        equ.setTemUnit("0");
        //底座LED背光的控制方式  0：自动 1：手动
        equ.setLightMode("1");
        //底座体温异常提醒 0：仅屏幕提醒  1：声光提醒  2：关闭
        equ.setExceptionRemind("0");
        return equMapper.insertEqu(equ);

    }

    /**
     * 修改设备管理
     *
     * @param equ 设备管理
     * @return 结果
     */
    @Override
    public int updateEqu(Equ equ)
    {
        equ.setUpdateTime(DateUtils.getNowDate());
        return equMapper.updateEqu(equ);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquByIds(String[] ids)
    {
        return equMapper.deleteEquByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquById(Long id)
    {
        return equMapper.deleteEquById(id);
    }

    @Override
    public List<EquVo> getListByUserId(EquVo equVo) {
        List<EquVo> eList = equMapper.getListByUserId(equVo);
        return eList;
    }

    @Override
    public AjaxResult getTotalCount(Equ equ) {
        Map<String,Object> m = new HashMap<>();
        QueryWrapper<Equ> qw = new QueryWrapper();
        //总数量
        Integer c = equMapper.selectCount(qw);
        m.put("total",c);
        //绑定的总数量
        Integer b = kernelUserEquMapper.selectTotalCount();
        m.put("bDingCount",b);
        //销售数量
        qw.eq("is_lease",2);
        Integer sale = equMapper.selectCount(qw);
        m.put("saleCount",b);
        return AjaxResult.success(m);
    }

    @Override
    public AjaxResult getYearSaleCount(Equ equ) {
        Map<String,Object> m = new HashMap<>();
        //年度销售数量
        List<Map<String,Object>> yearSale = equMapper.getYearSaleCount(equ);
        m.put("YearSale",yearSale);
        //年度绑定数量
        List<Map<String,Object>> yearBd = kernelUserEquMapper.getYearBdCount();
        m.put("yearBd",yearBd);
        //年度客户反馈数量
        List<Map<String,Object>> fkCount = kernelPendiFkMapper.getYearFk();
        m.put("fkCount",fkCount);
        //年度型号销售数量
        List<Map<String,Object>> yearModelSale = equMapper.getYearModelSale(equ);
        m.put("yearModelSale",yearModelSale);
        return AjaxResult.success(m);
    }

    @Override
    public List<Map<String, Object>> getIndexDt() {
        List<Map<String, Object>> mp =  equMapper.getIndexDt();
        mp.forEach(x->{
            Map<String,Object> m = new HashMap<>();
            m.put("className","AMap.LngLat");
            String lgt = "";
            String lat = "";
            if(x.get("lgt")!=null){
                lgt = x.get("lgt").toString();
            }
            if(x.get("lat")!=null){
                lat = x.get("lat").toString();
            }
            if(StringUtils.isNotEmpty(lgt)){
                m.put("lng",lgt);
            }else {
                m.put("lng","");
            }
            if(StringUtils.isNotEmpty(lat)){
                m.put("lat",lat);
            }else {
                m.put("lat","");
            }
            x.put("county","全国");
            x.put("lnglat",m);
        });
        return mp;
    }

    @Override
    public List<LunBo> getLunbo() {
        List<LunBo> list = equMapper.getLunbo();
        return list;
    }

    @Override
    public Map<String, Object> getBasicData(String mode) {
        Map<String,Object> mp = new HashMap<>();
        //查询设备总数
        QueryWrapper<Equ> qw = new QueryWrapper();
        int to = equMapper.selectCount(qw);
        mp.put("total",to);
        //绑定的总数量
        Integer b = kernelUserEquMapper.selectTotalCount();
        mp.put("bDingCount",b);
        //反馈数量
        Integer fkc = fkMapper.selectTotalCount();
        mp.put("fkCount",fkc);
        //查询医生总数量
        Integer doct = userMapper.selectDocCount();
        mp.put("doctorCount",doct);
        //查询患者总数量
        Integer patc = userMapper.selectPatientCount();
        mp.put("patientCount",patc);
        return mp;
    }

    @Override
    public Map<String, Object> getIndexTb() {
        Map<String,Object> mp = new HashMap<>();
        //设备型号占比
        List<Map<String,Object>> ml = equMapper.selectByModel();
        mp.put("modelData",ml);
        //本年度每月新增数量
        List<Map<String,Object>> elist = equMapper.selectByMOnth();
        mp.put("monthData",elist);

        //本年度每月反馈次数占比
        List<Map<String,Object>> clist = equMapper.selectFkByMOnth();
        mp.put("csData",clist);

        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(4); // 设置百分数精确度2即保留两位小数
        //查询每月测量的记录数
        List<Map<String,Object>> rlp =recordMapper.selectCeliangCount();
        mp.put("reList",rlp);
        return mp;
    }

    @Override
    public void updateEquDataBySn(Map map) {
        //底座
        String diSn = (String) map.get("diSn");
        String diEle = (String) map.get("diEle");
        String diVersion = (String) map.get("diVersion");
        String wet = (String) map.get("wet");
        //体温贴
        String tiSn = (String) map.get("tiSn");
        String tiEle = (String) map.get("tiEle");
        String tiVersion = (String) map.get("tiVersion");

        //修改底座
        Equ e = new Equ();
        e.setEquipmentSn(diSn);
        e.setElectic(diEle);
        e.setVersion(diVersion);
        e.setWet(wet);
        if(ObjectUtil.isNotEmpty(e)){
            equMapper.updateEquBySn(e);
        }
        //修改体温贴
        Equ te = new Equ();
        te.setEquipmentSn(tiSn);
        te.setElectic(tiEle);
        te.setVersion(tiVersion);
        if(ObjectUtil.isNotEmpty(te)){
            equMapper.updateEquBySn(te);
        }

    }

    @Override
    public void updateTionEqu(Equ equ) {
        equMapper.updateEquBySn(equ);
    }

    @Override
    public Equ selectEquBySn(String diSn) {
        Equ equ = equMapper.selectEquBySn(diSn);
        return equ;
    }


}
